package com.cms.clash.util;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.ui.Model;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * TemplateUtil
 * @author cms
 * @date 2024-07-28
*/
public class TemplateUtil {
    public static String TEMPLATE_INDEX = "index";
    public static String TEMPLATE_LIST = "list";
    public static String TEMPLATE_DETAIL = "detail";
    public static String TEMPLATE_PAGE404 = "404";
    public static String TEMPLATE_PAGE500 = "500";
    public static String TEMPLATE_SEARCH = "search";

    // 缓存相关
    public static Cache<Object, Object> cache = Caffeine.newBuilder()
            .initialCapacity(1000)
            .maximumSize(10000)
            .expireAfterWrite(1800, TimeUnit.SECONDS)
            .expireAfterAccess(1800, TimeUnit.SECONDS)
            .build();

    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    public static SimpleDateFormat sdfFull = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 十位时间戳转yyyy-MM-dd
     * @param time
     * @return
     */
    public static String tenTimeToString(Long time) {
        Date date = new Date(time * 1000);
        return sdf.format(date);
    }

    /**
     * 生成随机的uuid
     * @param num
     * @return
     */
    public static String makeRandUuid(Integer num) {
        String arr[] = new String[]{"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","p","q","r","s","t","u","v","w","x","y","z"};
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < num; i++) {
            sb.append(arr[new Random().nextInt(arr.length)]);
        }
        return sb.toString();
    }

    /**
     * 设置公共信息
     * @param model
     */
    public static void setSiteInfoMap(Model model) {
        model.addAttribute("site_title_simple", "免费clash节点|科学上网");
        model.addAttribute("site_title", "免费clash节点|科学上网 - 免费公益SSR/V2ray/Shadowrocket/Clash节点/小火箭订阅链接|科学上网|免费梯子");
        model.addAttribute("site_keywords", "clash节点,小火箭,免费节点,科学上网,免费梯子,免费clash节点,免费加速器,clash for windows,clash免费节点,clash github");
        model.addAttribute("site_description", "免费公益SSR/V2ray/Shadowrocket/Clash节点/小火箭订阅链接|科学上网|免费梯子");
    }

    /**
     * 分页工具
     * @param pageUrl
     * @param pageIndex
     * @param pageSize
     * @param totalCount
     * @return string
     */
    public static String getPageText(String pageUrl, int pageIndex, int pageSize, long totalCount) {
        Long maxPage = ((totalCount % pageSize) == 0) ? (totalCount / pageSize) : (totalCount / pageSize) + 1;
        int pageCount = maxPage.intValue();
        StringBuilder builder = new StringBuilder("");
        // 翻页越界
        if (pageIndex > pageCount) {
            return builder.toString();
        }
        // 只有一页就不显示了
        if (totalCount <= pageSize) {
            return builder.toString();
        }
        if (pageIndex <= 0) {
            pageIndex = 1;
        }

        builder.append("<div class='pagination'><ul>");
        int start;
        if (pageIndex > 1) {
            String currUrl = pageUrl.replace("@pageNum", "1");
            builder.append("<li><a href='").append(currUrl).append("' title='首页'><<</a></li>");
        }
        if (pageIndex > 5) {
            int prevPages = pageIndex - 9;
            if (prevPages < 1) {
                prevPages = 1;
            }
            start = pageIndex - 2;
            String currUrl = pageUrl.replace("@pageNum", prevPages + "");
            builder.append("<li><a href='").append(currUrl).append("' title='向前5页'>...</a></li>");
        } else {
            start = 1;
        }
        int end = pageIndex + 2;
        if (end > pageCount) {
            end = pageCount;
        }
        for (int index = start; index <= end; index++) {
            String currUrl = pageUrl.replace("@pageNum", index + "");
            if (pageIndex == index) {
                builder.append("<li class='active'><a href='")
                        .append(currUrl)
                        .append("'><b>").append(index)
                        .append("</b></a></li>");
            } else if (index != 0) {
                builder.append("<li><a href='").append(currUrl).append("'>").append(index).append("</a></li>");
            }
        }
        if (end < pageCount) {
            int endFive = end + 4;
            if (end > pageCount) {
                endFive = pageCount;
            }
            String currUrl = pageUrl.replace("@pageNum", endFive + "");
            builder.append("<li><a href='").append(currUrl).append("' title='向后5页'>...</a><li>");
        }
        if (pageIndex < pageCount) {
            String currUrl = pageUrl.replace("@pageNum", pageCount + "");
            builder.append("<li><a href='").append(currUrl).append("' title='末页'>>></a></li>");
        }
        builder.append("</ul></div>");
        return builder.toString();
    }

}
